/*
 * @Author: Wushiqi
 * @Descripttion: 消息提示
 * @Date: 2020-09-18 15:24:30
 * @LastEditor: Wushiqi
 * @LastEditTime: 2021-02-23 10:02:38
 */

// 重置message，防止重复点击重复弹出message弹框
import { Message, MessageBox } from 'element-ui'
const typeList = ['error', 'success', 'info', 'warning']
let messageInstance = null
const resetMessage = (options) => {
  if (messageInstance) {
    messageInstance.close()
  }
  messageInstance = Message(options)
}
let messageBoxInstance = null
const resetMessageBox = (options, title, callback) => {
  if (messageBoxInstance) {
    MessageBox.close()
  }
  messageBoxInstance = MessageBox.alert(options, title, callback)
}

typeList.forEach(type => {
  resetMessage[type] = options => {
    if (typeof options === 'string') {
      if (type === 'error' || type === 'warning') {
        options = {
          message: options,
          duration: 7000,
          showClose: true
        }
      } else {
        options = {
          message: options,
          showClose: true
        }
      }
    }
    options.type = type
    return resetMessage(options)
  }
})
const message = resetMessage
const messageAlert = resetMessageBox

export {
  message,
  messageAlert
}
